package com.derbtec.refale.activities;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.derbtec.refale.R;
import com.derbtec.refale.models.Event;
import com.derbtec.refale.transaction.SetEventAsyncTask;

public class AddEventActivity extends Activity {
	
	private EditText eteventtitle, eteventdescription, eteventplace, eteventstartdate, eteventstarttime, eteventenddate, eteventendtime;
	private Button savebutton;
	
    static final int DATE_DIALOG_START = 0;
    static final int DATE_DIALOG_END = 1;
    static final int TIME_DIALOG_START = 2;
    static final int TIME_DIALOG_END = 3;
    
    private Date startdate = new Date();
    private Date enddate = new Date();
    
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_event);
        
        Calendar c = Calendar.getInstance();
        startdate = c.getTime();
        enddate = c.getTime();
        
        eteventtitle = (EditText)findViewById(R.id.etEventtitle);
        eteventdescription = (EditText)findViewById(R.id.etEventDescription);
        eteventplace = (EditText)findViewById(R.id.etEventPlace);
        eteventstartdate = (EditText)findViewById(R.id.etEventStartDate);
        eteventstarttime = (EditText)findViewById(R.id.etEventStartTime);
        eteventenddate = (EditText)findViewById(R.id.etEventEndDate);
        eteventendtime = (EditText)findViewById(R.id.etEventEndTime);
        savebutton = (Button)findViewById(R.id.etEventSave);
        
        eteventstartdate.setText(startdate.getDate()+"."+startdate.getMonth()+"."+(startdate.getYear()+ 1900));
        eteventenddate.setText(enddate.getDate()+"."+enddate.getMonth()+"."+(enddate.getYear()+ 1900));
        eteventstarttime.setText(startdate.getHours()+":"+startdate.getMinutes());
        eteventendtime.setText(enddate.getHours()+":"+enddate.getMinutes());
        
        addListenerOnDateField();
        addListenerOnTimeField();
        
        savebutton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//				Date startDate = null;
//				Date endDate = null;
//				try {
//					startDate = sdf.parse(eteventstartdate.getText().toString() + " " + eteventstarttime.getText().toString());
//					endDate = sdf.parse(eteventenddate.getText().toString() + " " + eteventendtime.getText().toString());
//				} catch (ParseException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				
				Event event = new Event(
						eteventtitle.getText().toString(),
						eteventplace.getText().toString(),
						eteventdescription.getText().toString(),
						startdate,
						enddate);
				SetEventAsyncTask savetask = new SetEventAsyncTask(AddEventActivity.this, event);
				savetask.execute();
				

			}
		});
        
        
    }
    
	public void addListenerOnDateField() {
		 
		eteventstartdate.setOnTouchListener(new OnTouchListener() {
			@SuppressWarnings("deprecation")
			public boolean onTouch(View arg0, MotionEvent arg1) {
				showDialog(DATE_DIALOG_START);
				return true;
			}
 
		});
		
		eteventenddate.setOnTouchListener(new OnTouchListener() {
			@SuppressWarnings("deprecation")
			public boolean onTouch(View arg0, MotionEvent arg1) {
				showDialog(DATE_DIALOG_END);
				return true;
			}
 
		});
 
	}
	public void addListenerOnTimeField() {
		 
		eteventstarttime.setOnTouchListener(new OnTouchListener() {
			@SuppressWarnings("deprecation")
			public boolean onTouch(View arg0, MotionEvent arg1) {
				showDialog(TIME_DIALOG_START);
				return true;
			}
 
		});
		
		eteventendtime.setOnTouchListener(new OnTouchListener() {
			@SuppressWarnings("deprecation")
			public boolean onTouch(View arg0, MotionEvent arg1) {
				showDialog(TIME_DIALOG_END);
				return true;
			}
 
		});
 
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected Dialog onCreateDialog(int id) {
		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
		switch (id) {
		case DATE_DIALOG_START:
		   // set date picker as current date
		   return new DatePickerDialog(parentActivity, datePickerListenerStart, (startdate.getYear()+ 1900), startdate.getMonth(), startdate.getDate());
		   
		case DATE_DIALOG_END:
			   // set date picker as current date
			   return new DatePickerDialog(parentActivity, datePickerListenerEnd, (enddate.getYear()+ 1900), enddate.getMonth(), enddate.getDate());
			   
		case TIME_DIALOG_START:
			   // set date picker as current date
			   return new TimePickerDialog(parentActivity, timePickerListenerStart, startdate.getHours(), startdate.getMinutes(), true);
			   
		case TIME_DIALOG_END:
			   // set date picker as current date
			   return new TimePickerDialog(parentActivity, timePickerListenerEnd, enddate.getHours(), enddate.getMinutes(), true);

		}
		return null;
	}
	
	
	private DatePickerDialog.OnDateSetListener datePickerListenerStart 
    = new DatePickerDialog.OnDateSetListener() {


	@SuppressWarnings("deprecation")
	public void onDateSet(DatePicker view, int dyear, int monthOfYear,
		int dayOfMonth) {
	
	startdate.setYear(dyear);
	startdate.setMonth(monthOfYear);
	startdate.setDate(dayOfMonth);
	
	eteventstartdate.setText(startdate.getDate()+"."+startdate.getMonth()+"."+(startdate.getYear()+ 1900));
	
	}
	};
	private DatePickerDialog.OnDateSetListener datePickerListenerEnd 
	= new DatePickerDialog.OnDateSetListener() {
	
	@SuppressWarnings("deprecation")
	public void onDateSet(DatePicker view, int dyear, int monthOfYear,
	int dayOfMonth) {
	
		enddate.setYear(dyear);
		enddate.setMonth(monthOfYear);
		enddate.setDate(dayOfMonth);
	
		eteventenddate.setText(enddate.getDate()+"."+enddate.getMonth()+"."+(enddate.getYear()+ 1900));
	
	}
	};
	
	private TimePickerDialog.OnTimeSetListener timePickerListenerStart
	= new TimePickerDialog.OnTimeSetListener() {
		
		@SuppressWarnings("deprecation")
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			
			startdate.setHours(hourOfDay);
			startdate.setMinutes(minute);
			
			eteventstarttime.setText(startdate.getHours()+":"+startdate.getMinutes());
			
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timePickerListenerEnd
	= new TimePickerDialog.OnTimeSetListener() {
		
		@SuppressWarnings("deprecation")
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			
			enddate.setHours(hourOfDay);
			enddate.setMinutes(minute);
			
			eteventendtime.setText(enddate.getHours()+":"+enddate.getMinutes());
			
		}
	};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_event, menu);
        return true;
    }
	
	public void reload() {
		  
	     Intent intent = new Intent(this,this.getClass());
	     TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	     parentActivity.finishFromChild(AddEventActivity.this);
	     parentActivity.startChildActivity("AddEventActivity", intent);
	 }
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * Overrides the default implementation for KeyEvent.KEYCODE_BACK 
     * so that all systems call onBackPressed().
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
            parentActivity.onBackPressed();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
	
}
